package JFrame;

import JFrame.Admin.AdminMainFrame;
import JFrame.User.UserJFrame;

import serive.AdminService;
import serive.AdminServiceImpl;
import serive.UserService;
import serive.UserServiceImpl;


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @className: loginJFrame
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/27
 **/
public class LoginJFrame extends JFrame {
    JLabel l_name, l_type, l_password;
    JTextField t_name;
    JPasswordField t_password;
    JComboBox<String> c_type;
    JButton b_login, b_reset, b_register;
    UserService userService = new UserServiceImpl();
    AdminService adminService =new AdminServiceImpl();
    JFrame jFrame = this;
    public LoginJFrame() {
        this.setTitle("快递派送系统——登录");
        this.setSize(400, 550);
        this.setLocationRelativeTo(null);//窗体居中显示
        init();
        SnowJPanel sjp = new SnowJPanel();
        sjp.setBounds(20,10,350,200);
        this.add(sjp);
        Thread thread = new Thread(sjp);
        thread.start();
        this.setVisible(true);
    }

    public void init() {
        setResizable(false);
        this.setLayout(null);
        l_name = new JLabel("用户名", JLabel.CENTER);
        l_type = new JLabel("用户类型", JLabel.CENTER);
        l_password = new JLabel("用户密码", JLabel.CENTER);
        t_name = new JTextField();
        t_password = new JPasswordField();
        c_type = new JComboBox<String>();
        c_type.addItem("普通用户");
        c_type.addItem("管理员");

        b_login = new JButton("登录");
        b_login.setBackground(new Color(238, 231, 231));
        b_login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean isLogin = userService.login(t_name.getText(), t_password.getText());
                boolean isAdminLogin = adminService.login(t_name.getText(), t_password.getText());
                if (isLogin&&c_type.getSelectedItem().equals("普通用户")){
                    new UserJFrame(t_name.getText());
                }else if (isAdminLogin&&c_type.getSelectedItem().equals("管理员")){
                    new AdminMainFrame();
                }else {
                    //  （2）如果为非法用户
                    JOptionPane.showMessageDialog(jFrame.getContentPane(),
                            "用户名或密码错误", "！！！",JOptionPane.WARNING_MESSAGE);
                }
           }

        });
        b_reset = new JButton("重置");
        b_reset.setBackground(new Color(173, 167, 159));
        b_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    Tools.emptying(t_name,t_password);
            }
        });
        b_register = new JButton("点我注册");
        b_register.setBackground(new Color(238, 231, 231));
        b_register.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               new RegisterJFrame();

            }

        });

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(3, 2, 5, 5));
        p1.add(l_name);
        p1.add(t_name);
        p1.add(l_type);
        p1.add(c_type);
        p1.add(l_password);
        p1.add(t_password);
        p1.setBounds(5, 225, 350, 185);
        this.add(p1);
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(1, 3, 5, 5));
        p2.add(b_login);
        p2.add(b_reset);
        p2.add(b_register);
        p2.setBounds(5, 425, 350, 50);
        this.add(p2);

        Tools.setFonts(18,1,l_name, l_type, l_password,b_login, b_reset, b_register);
    }


}
class SnowJPanel extends JPanel implements Runnable {

    int[] x = new int[300];
    int[] y = new int[300];
    int[] a = new int[300];

    public int random(int a){
        return (int)(Math.random()*a);
    }

    public SnowJPanel(){
        for (int i = 0; i < 100; i++) {
            x[i] = new Random().nextInt(800) + 1;
            y[i] = new Random().nextInt(600) + 1;
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //设置背景色
        setBackground(new Color(150, 140, 140, 242));
        //设置画笔的背景
        g.setColor(Color.white);
        //画星星飘落
        for (int i = 0; i < 300; i++) {
            g.drawString("*",x[i],y[i]);
            for (int j = 0; j < a[i]; j++) {
                g.drawString("*",x[i],600 - j*3);
            }
        }

        //画月亮
        g.fillOval(30,20,40,40);
        g.setColor(new Color(150, 140, 140, 242));
        g.fillOval(50,20,40,40);


    }

    @Override
    public void run() {
        while(true){
            for (int i = 0; i < y.length; i++) {
                if(y[i]<=600){
                    y[i] ++;
                }else{
                    a[i] ++;
                    y[i] = 0;
                }
            }
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
